package com.example.pokemon_chat_room.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.R;
import com.example.pokemon_chat_room.pojo.MyItemsDto;
import com.example.pokemon_chat_room.pojo.MyPokemonDto;
import com.example.pokemon_chat_room.request.OkHttp;
import com.example.pokemon_chat_room.util.MyImageView;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class MyItemsAdapter extends RecyclerView.Adapter<MyItemsAdapter.ViewHolder> {



    private List<MyItemsDto> myItemsList;
    private View view;
    private Context context;

    public MyItemsAdapter(Context context, List<MyItemsDto> myItemsList) {
        this.context = context;
        this.myItemsList = myItemsList;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private MyImageView items_img;
        private TextView items_txt;
        private TextView items_count;
        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            items_img = view.findViewById(R.id.items_img);
            items_txt = view.findViewById(R.id.items_txt);
            items_count = view.findViewById(R.id.items_count);

        }

    }


    @NonNull
    @Override
    public MyItemsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_items_item_layout, parent, false);
        MyItemsAdapter.ViewHolder holder = new MyItemsAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyItemsAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.items_img.setImageURL("poke/items/ys/" + myItemsList.get(position).getImage());
        holder.items_txt.setText(myItemsList.get(position).getName());
        String itemsCount = String.valueOf(myItemsList.get(position).getItemsCount());

        holder.items_count.setText(itemsCount);

        holder.items_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myItemsList.get(position).getType() == 0) {
                    return;
                }
                if (myItemsList.get(position).getItemsCount() == 0) {
                    return;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示：");
                builder.setMessage("请选择您的操作");
                builder.setIcon(R.mipmap.ic_launcher_round);
                //点击对话框以外的区域是否让对话框消失
                builder.setCancelable(true);
                //设置正面按钮
                builder.setPositiveButton("使用", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OkHttp okHttp = new OkHttp();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                JSONObject jsonObject = new JSONObject();
                                SharedPreferences mShared = Objects.requireNonNull(context).getSharedPreferences("user", MODE_PRIVATE);
                                String token = mShared.getString("token", "");
                                jsonObject.put("token", token);
                                jsonObject.put("itemsBaseId", myItemsList.get(position).getId());
                                try {
                                    JSONObject res = okHttp.Post(context, "items-base/addBuff", jsonObject);
                                    ((Activity) context).runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(context, res.getString("message"), Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                                            myItemsList.get(position).setItemsCount(myItemsList.get(position).getItemsCount() - 1);
                                        }
                                    });
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }).start();
                    }
                });
                builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return myItemsList.size();
    }
}
